package edu.ccut.saturn.studio.database.popup.action;

import java.sql.Connection;
import java.util.List;


import edu.ccut.saturn.studio.core.util.action.SaturnAction;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnDialogUtil;
import edu.ccut.saturn.studio.database.model.DBConnection;
import edu.ccut.saturn.studio.database.model.SaturnTree;
import edu.ccut.saturn.studio.database.model.DBConnection.DBConnectionListener;
import edu.ccut.saturn.studio.database.ui.DBConnectView;

/**
 * 展开连接
 * 
 * @author 谷钰
 * @version 1.0 2008.12.06
 * 
 */
public class ConnectAction extends SaturnAction {

	public void run() {
		List<SaturnTree> selecteds = DBConnectView.getInstance()
				.getCurrentSelections();

		if (selecteds != null) {
			for (SaturnTree selected : selecteds) {
				if (selected instanceof DBConnection) {
					final DBConnection dbConnection = (DBConnection) selected;
				
					dbConnection.getConnectionAsync(new DBConnectionListener() {
						public void onGetConnection(Connection connection) {
							getShell().getDisplay().asyncExec(new Runnable() {
								public void run() {
									try {
										DBConnectView.getInstance().expand(
												dbConnection);
									} catch (Exception e) {
										SaturnDialogUtil.openError(getShell(), e
												.getMessage());
									}
								}
							});
						}
					});
				}
			}
		}
	}
}
